/*
============================================================================
 Name        : RfsEngine.cpp
 Author      : Chenzs
 Version     :
 Copyright   : Joyhoo.com.cn
 Description : CRfsEngine implementation
============================================================================
*/

#include "RfsEngine.h"
#include <BAUTILS.H>

CRfsEngine::CRfsEngine()
{
	// No implementation required
}


CRfsEngine::~CRfsEngine()
{
	if(iFileList)
		delete iFileList;
	if(iDirList)
		delete iDirList;
	iFs.Close();
}

CRfsEngine* CRfsEngine::NewLC()
{
	CRfsEngine* self = new (ELeave)CRfsEngine();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CRfsEngine* CRfsEngine::NewL()
{
	CRfsEngine* self=CRfsEngine::NewLC();
	CleanupStack::Pop(); // self;
	return self;
}

void CRfsEngine::ConstructL()
{
	User::LeaveIfError(iFs.Connect());
}

//��ȡ�ļ��б�
TInt CRfsEngine::GetFileList(const TDesC& aPath)
{
	if(iFileList)
	{
		delete iFileList;
		iFileList = NULL;
	}

	if(iDirList)
	{
		delete iDirList;
		iDirList = NULL;
	}

	TInt result = iFs.GetDir(aPath, 
							 KEntryAttNormal | KEntryAttHidden | KEntryAttSystem,
                             ESortByName | EDirsFirst | EAscending,
                             iFileList,
							 iDirList
							 );
	return result;

}

//�ļ���
TInt CRfsEngine::FileCount()
{
	if(iFileList)
		return iFileList->Count();
	else
		return 0;
}

//�ļ����
const TDesC & CRfsEngine::GetFileName(TInt aFileIndex)
{
	if(iFileList && aFileIndex < iFileList->Count())
		return iFileList->operator[](aFileIndex).iName;
	else
		return KNullDesC;
}

//·������ڴ���֮
void CRfsEngine::EnsurePathExistsL(const TDesC &aFileName)
{
	BaflUtils::EnsurePathExistsL(iFs, aFileName);
}

//�ļ����Ƿ����
TBool CRfsEngine::FolderExist(const TDesC &aFolderName)
{
	return BaflUtils::FolderExists(iFs, aFolderName);
}

//�ļ��Ƿ����
TBool CRfsEngine::FileExist(const TDesC &aFileName)
{
	return BaflUtils::FileExists(iFs, aFileName);
}

//·���Ƿ����
TBool CRfsEngine::PathExist(const TDesC &aPath)
{
	return BaflUtils::PathExists(iFs,aPath);
}

//˽��·��
TInt CRfsEngine::PrivatePath(TDes &aPath)
{
	return iFs.PrivatePath(aPath);
}

//ɾ���ļ�
TInt CRfsEngine::DeleteFile(const TDesC &aFileName)
{
	return iFs.Delete(aFileName);
}

//�����ļ�
TInt CRfsEngine::Replace(const TDesC& aFileName, const TDesC8& aDes)
{
	RFile file;
	//open file for write
	TInt result = file.Replace(iFs, aFileName, EFileWrite|EFileShareAny );
	if(result == KErrNone)
	{
		result = file.Write(aDes);
		if(result == KErrNone)
			result = file.Flush();
		file.Close();
	}
	
	return result;

}

//get RFs
RFs CRfsEngine::RFS()
{
	return iFs;
}
